<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    #left{
        float: left;
        width: 30%;
        height: 500px;
        margin-left: 200px;
    }
    #right{
        float: right;
        width: 30%;
        height: 500px;
    }
    #top{
        margin-top: 50px;
        float: top;
        width: 30%;
        height: 250px;
    }
    #content{
        border: aquamarine 1px solid;
        width: 100%;
        height: 300px;
    }
    #input{
        margin-top: 20px;
        width: 100%;
        height: 200px;
    }
    #input input{
        width: 100%;
        height: 100px;
    }
    #input button{
        float: right;
    }
    #mes_left{
        float: left;

    }
    #mes_right{
        float: right;
        width: 50%;
        text-align: right;
    }
</style>
<body>
<script src="statics/jquery-1.12.4/jquery-1.12.4.js"></script>
<h3 style="text-align: center" id="username"></h3>
<div>
    <div id="left">
        <h4 id="new"></h4>
        <div id="content">

        </div>
        <div id="input">
            <input type="text" id="input_text">
            <button id="submit">发送</button>
        </div>
    </div>
    <div id="right">
        <div id="top">
            <p>在线的好友</p>
            <div id="hylist">

            </div>
        </div>
        <div id="bottom">
            <p>系统广播</p>
            <div id="xtlist">
            </div>
        </div>
    </div>
</div>
<script>
    var username;
    var toName;
    function showChat(name){
        // alert("dsaad");
        toName = name;
        //清空聊天区
        $("#content").html("");
        //显示"正在和谁聊天"
        $("#new").html("当前正与"+toName+"聊天");
        //sessionStorage
        var chatData = localStorage.getItem(toName);
        if (chatData != null){
            //将聊天记录渲染到聊天区
            $("#content").html(chatData);
        }

        //是否有离线消息
    }

    $(function () {
        $.ajax({
            url:"getUsername",
            success:function (res) {
                username = res;
                $("#username").html("用户："+ username +"<span>在线</span>");
                // $("#username").html("用户：123<span>在线</span>");
            },
            async:false //同步请求，只有上面好了才会接着下面
        });

        //创建webSocket对象
        var ws = new WebSocket("ws://localhost:9000/chat");
        //给ws绑定事件，建立socket连接
        ws.onopen = function (ev) {
            //在建立连接后，显示在线用户信息
            $("#username").html("用户："+ username +"<span>在线</span>");
        }
        //接受服务端推送的消息后触发
        ws.onmessage = function (ev) {
            //获取服务端推送过来的消息
            var datastr = ev.data;
            //将datastr转换为 json对象
            var res = JSON.parse(datastr);
            //判断是否是系统消息
            if(res.system){
                //1.好友列表
                //2.系统广播
                var names = res.message;
                var userlistStr = ""; //好友列表
                var broadcastListStr = ""; //系统广播

                for (var name of names){
                    if (name != username){
                        userlistStr += "<a onclick='showChat(\""+name+"\")'>"+ name +"</a></br>";
                        broadcastListStr += "<p>"+ name +"上线了</p>";
                    }
                };
                //渲染好友列表和系统广播
                $("#hylist").html(userlistStr);
                $("#xtlist").html(broadcastListStr);

            }else {
                //不是系统消息
                //将服务端推送的消息进行展示
                var str = "<span id='mes_left'>"+ res.message +"</span></br>";
                //
                if (toName == res.fromName) {
                    $("#content").append(str);
                }

                var chatdata = localStorage.getItem(res.fromName);
                if (chatdata != null){
                    str = chatdata + str;
                }
                localStorage.setItem(res.fromName,str);

            }
        }

        ws.onclose = function (ev) {
            //关闭socket连接，显示离线用户信息
            $("#username").html("用户："+ username +"<span>离线</span>");
        }

        //发送消息
        $("#submit").click(function () {
            //获取输入的内容
            var data = $("#input_text").val();
            //清空输入区的内容
            $("#input_text").val("");
            var json = {"toName": toName ,"message": data};
            //将数据展示在聊天区
            var str = "<span id='mes_right'>"+ data +"</span></br>";
            $("#content").append(str);

            //
            var chatData = localStorage.getItem(toName);
            if (chatData != null){
                str = chatData + str;
            }
            localStorage.setItem(toName,str);
            //发送数据给服务端
            ws.send(JSON.stringify(json));
        });
    })
</script>
</body>
</html>
